import { add, detail,list } from "../../api/index.js";
var app = getApp();
Page({
  data: {
    id: 0,
    foodtype: 0,
    num: 1,
    buycar_num: 0,
    totalMoney: 0,
    block: false,
    price: 12.43,
    arr: [
      { id: 0, value: "香辣味" },
      { id: 1, value: "盐焗味" },
      { id: 2, value: "蒜香味" },
      { id: 3, value: "姜葱味" },
    ],
    detail: {},
    evaluateList: [],
    evaluateLength: "",
  },
  onLoad: function (options) {
    this.setData({
      id: options.id,
    });
    this.setData({
      buycar_num: app.globalData.buycar_num,
      totalMoney: app.globalData.totalMoney,
    });
    
    Promise.all([this.getDetail()]).then(() => {
      this.getEvaluateList();
    })
    
  },
  async getEvaluateList() {
    const params = {
      pageNum: 1,
      pageSize: 100,
      goodsId: this.data.detail.id,
    };
    const table = 'evaluate';
    const res = await list(params,table)
  //  if (res.code === 200) {
  //     this.setData({
  //       evaluateList: res.rows,
  //       evaluateLength: res.rows.length
  //     })
    
  //  }
  if (res.code === 200) {
    const evaluateList = res.rows.map(item => {
      return {
        ...item,
        evaluatePhotos: item.evaluatePhoto 
            ? item.evaluatePhoto.split(',') 
            : []
    };
    });
    this.setData({
      evaluateList: evaluateList,
      evaluateLength: res.rows.length
    });
  }
  },
  async getDetail() {
    const id = this.data.id;
    const tableName = "goods";
    const res = await detail(id, tableName);
    if (res.code === 200) {
      const data = {
        id: res.data.id,
        img: res.data.goodPhoto,
        name: res.data.goodName,
        price: res.data.originalPrice,
        goodType: res.data.goodType,
        goodImg: res.data.goodImg,
        salesVolume: res.data.salesVolume,
        reviewRate: res.data.reviewRate,
        message: res.data.remark,
        message2:
          "已售" + res.data.salesVolume + "好评率" + res.data.reviewRate,
        message3: res.data.standby1 ? JSON.parse(res.data.standby1) : "",
      };
      this.setData({
        detail: data,
      });
    }
  },
  resetNum: function (e) {
    var type = e.currentTarget.dataset.id;
    this.setData({
      foodtype: type,
    });
  },
  reduce: function () {
    if (this.data.num > 0) {
      app.globalData.buycar_num--;
      app.globalData.totalMoney -= this.data.price;
      var totalMoney = app.globalData.totalMoney.toFixed(2);
      this.setData({
        num: this.data.num - 1,
        buycar_num: app.globalData.buycar_num,
        totalMoney: totalMoney,
      });
    }
  },
  add: function () {
    app.globalData.buycar_num++;
    app.globalData.totalMoney += this.data.price;
    var totalMoney = app.globalData.totalMoney.toFixed(2);
    this.setData({
      num: this.data.num + 1,
      buycar_num: app.globalData.buycar_num,
      totalMoney: totalMoney,
    });
  },
  close: function () {
    this.setData({
      block: false,
    });
  },
  open: function () {
    this.setData({
      block: true,
    });
  },
  submit: function () {
    var that = this;
    var i = this.data.foodtype;
    var id; //哪种食物
    wx.request({
      url: "",
      data: {
        type: that.data.arr[i].value,
        num: that.data.num,
      },
      success: function (res) {
        that.setData({
          block: false,
        });
        console.log(res.data);
      },
    });
  },
  buyNow: function () {
    wx.navigateTo({
      url: "../submitOrder/submitOrder",
    });
  },
  toAllEvaluate: function () {
    wx.navigateTo({
      url: "../allEvaluate/allEvaluate?goodId=" + this.data.detail.id,
    });
  },
  async toCart() {
    const data = {
      goodsId: this.data.id,
      number: this.data.num,
      userId: wx.getStorageSync("userInfo").userId,
      // foodtype: this.data.foodtype,
      price: this.data.totalMoney,
      standby1: 0,
      goodPhoto: this.data.detail.img,
      goodName: this.data.detail.name,
      // price: this.data.detail.price,
    };
    const res = await add(data, "cart");
    if (res.code === 200) {
      wx.showToast({
        title: "添加成功",
        icon: "success",
        duration: 2000,
      });
    }
  },
  toSubmit: function () {
    const data = [
      {
        id: this.data.id,
        num: this.data.num,
        foodtype: this.data.foodtype,
        totalMoney: this.data.totalMoney,
        img: this.data.detail.img,
        name: this.data.detail.name,
        price: this.data.detail.price,
      },
    ];
    wx.setStorage({
      key: "detailData",
      data: data,
      success: function () {
        wx.navigateTo({
          url: "../submitOrder/submitOrder",
        });
      },
    });
  },
});
